import 'package:get/get.dart';
import 'package:sgy_flutter/ble/errors.dart';
import 'package:sgy_flutter/extensions/ble_state.dart';
import 'package:sgy_flutter/extensions/get.dart';
import 'package:sgy_flutter/model/product_type.dart';
import 'package:sgy_flutter/widget/app_dialog.dart';

class OtherSettingsLogic extends GetxController with BleState {
  Rx<bool> reverseChain2ZeroOn = false.obs;
  Rx<bool> transportModeOn = false.obs;

  Rx<bool> reverseChain2ZeroVisible = false.obs;
  Rx<bool> transportModeVisible = false.obs;
  Rx<bool> deviceSettingVisible = false.obs;
  Rx<bool> deviceCheckVisible = true.obs;

  @override
  void onInit() {
    super.onInit();
    if (bleManager.deviceModel != null) {
      final deviceModel = bleManager.deviceModel!;
      printInfo(info: "deviceModel.isNewDevice:${deviceModel.isNewDevice}");
      if (deviceModel.isNewDevice == true) {
        switch (deviceModel.productType) {
          case ProductType.dls:
            transportModeVisible(deviceModel.firmwareNumber >= 1.91);
            deviceSettingVisible(true);
            break;
          case ProductType.axo:
            transportModeVisible(deviceModel.firmwareNumber >= 2.58);
            reverseChain2ZeroVisible(deviceModel.firmwareNumber >= 2.58);
            break;
          case ProductType.apm2:
            break;
          case ProductType.axpower:
            break;
          case ProductType.axocc:
            break;
          case ProductType.hiflow:
            break;
          case ProductType.jt:
            break;
          case ProductType.unknow:
            break;
        }
      } else if (deviceModel.isNewDevice == false) {
        switch (deviceModel.productType) {
          case ProductType.dls:
            transportModeVisible(deviceModel.firmwareNumber >= 4.8);
            break;
          case ProductType.axo:
            transportModeVisible(deviceModel.firmwareNumber >= 4.024);
            reverseChain2ZeroVisible(deviceModel.firmwareNumber >= 4.024);
            break;
          case ProductType.apm2:
            break;
          case ProductType.axpower:
            deviceCheckVisible(false);
            break;
          case ProductType.axocc:
            deviceCheckVisible(false);
            break;
          case ProductType.hiflow:
            break;
          case ProductType.jt:
            break;
          case ProductType.unknow:
            break;
        }
      }
    }
  }

  @override
  void onReady() {
    bleManager.getReverseChain2Zero().then((value) {
      reverseChain2ZeroOn(value);
    }, onError: (err) {
      printError(info: "$err");
    });
    super.onReady();
  }

  //只有axo，没有dls双边设备
  Future<bool> toggleReverseChain2ZeroOn() async {
    if (await bleManager.ensureDeviceConnected()) {
      try {
        await bleManager.setReverseChain2Zero(!reverseChain2ZeroOn.value);
        reverseChain2ZeroOn.toggle();
      } on EmptyWriteCharacteristicError catch (e) {
        showToast("请先移除充电线并重新连接".tr);
        return false;
      } catch (e) {
        printError(info: "$e");
      }
    }

    return reverseChain2ZeroOn.value;
  }

  Future<bool> openTransportMode() async {
    final result = await Get.dialog(
        AppDialog(
          title: "开启运输模式后，可减少运输时的电量消耗。".tr,
          message: "连接充电线充电，或连续转动曲柄10圈，可以退出运输模式。".tr,
          submit: "开启".tr,
          cancel: "取消".tr,
          onSubmit: () => Get.backLegacy(result: true),
        ),
        barrierDismissible: false);
    if (result == true) {
      try {
        await wrapperOperation(leftOperation: () async {
          await bleManager.openTransportMode();
        }, rightOperation: () async {
          await rightBleManager?.openTransportMode();
        });
        transportModeOn.toggle();
        if (bleManager.deviceModel != null) {
          bleManager.userDisconnect(bleManager.deviceModel!);
        }
        if (rightBleManager.deviceModel != null){
          rightBleManager.userDisconnect(rightBleManager.deviceModel!);
        }
      } on EmptyWriteCharacteristicError catch (e) {
        return false;
      } catch (e) {
        printError(info: "$e");
        showToast("$e");
        return false;
      }
    }

    return transportModeOn.value;
  }
}
